/*
 * @(#)PhoneNumberTransformer.java
 * Date 2013-08-16
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.pix.transformer;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.TEL;

import us.hornerscorners.lamppost.config.ModelConfig;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.model.common.CodedElement;
import us.hornerscorners.lamppost.model.common.PhoneNumber;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class PhoneNumberTransformer extends AbstractTransformer<TEL, PhoneNumber> {

    /** Field description */
    private static final String TEL_PREFIX = "tel:";

    /** Field description */
    @Inject
    private IHEConfig iheConfig;

    /**
     * Constructs ...
     *
     */
    public PhoneNumberTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param prpaTelecom
     *
     * @return
     */
    @Override
    public PhoneNumber transform(TEL prpaTelecom) {

        PhoneNumber result = null;

        if (prpaTelecom != null) {

            result = new PhoneNumber();

            String telstr = StringUtils.remove(prpaTelecom.getValue(), TEL_PREFIX);

            result.setValue(telstr);

            String usestr = HL7Utils.parseFirstString(prpaTelecom.getUse());

            if (StringUtils.isNotBlank(usestr)) {

                ModelConfig modelConfig = this.iheConfig.getModelConfig();

                CodedElement elem = new CodedElement();

                elem.setCode(usestr);
                elem.setCodeSystem(modelConfig.getPhoneNumberUseCodeSystem());
                result.setPurpose(elem);
            }
        }

        return result;
    }
}
